package com.wawayaya.bookreader.thumbnail;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;
import com.wawayaya.bookreader.DefaultSettings;
import com.wawayaya.util.ImageUtils;
import java.util.ArrayList;
import wawayaya.kids_iread.R;

public class BottomGalleryAdapter extends BaseAdapter {
	private ArrayList<BottomGalleryDataObj> mGalleryDataList;
	private final LayoutInflater mInflater;
	private int    mSelectedIndex = -1;
	private Context mContext;
	private   int       mThumbnailWidth = 0;
	private   int       mThumbnailHeight = 0;
	private Matrix mMatrix = new Matrix();
	private float     mRatio = 1.0f;
	
	public BottomGalleryAdapter(Context context, ArrayList<BottomGalleryDataObj> datalist) {
		mGalleryDataList = datalist;
		mInflater   = LayoutInflater.from(context);
		mContext = context;
		mThumbnailWidth = (int)mContext.getResources().getDimension(R.dimen.thumbnail_width);
		mThumbnailHeight = (int)mContext.getResources().getDimension(R.dimen.thumbnail_height);
		BottomGalleryDataObj firstpage = mGalleryDataList.get(0);
		if (firstpage != null && !TextUtils.isEmpty(firstpage.mThumb)) {
			BitmapFactory.Options options = new BitmapFactory.Options();
			options.inPreferredConfig = Bitmap.Config.RGB_565;
			options.inJustDecodeBounds = true;
			ImageUtils.safedecodebitmapfile(firstpage.mThumb, options);
			if (options.outWidth  > 0 && options.outHeight > 0) {
				 mRatio = (float)mThumbnailWidth / options.outWidth;
				mMatrix.reset();
				mMatrix.postScale(mRatio, mRatio);
			}
		}
		
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return Integer.MAX_VALUE;
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.reader_bottomgalleryitem, null);
            holder = new ViewHolder();
            holder.mThumbNail = (ThumbnailView)convertView.findViewById(R.id.thumbnail);
            //holder.mNormalMask = (ImageView)convertView.findViewById(R.id.normalmask);
            holder.mPageName = (TextView)convertView.findViewById(R.id.pagename);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }
        
        
        
        int index = position % mGalleryDataList.size();
        BottomGalleryDataObj obj = mGalleryDataList.get(index);
        
        int selected_index = mSelectedIndex % mGalleryDataList.size();
        if (index == selected_index) {
        	holder.mPageName.setBackgroundResource(R.drawable.thumbnail_mask_down);
        	 holder.mPageName.setTextColor(mContext.getResources().getColor(R.color.thumbnail_name_color_down));
        } else {
        	holder.mPageName.setBackgroundResource(R.drawable.thumbnail_mask);
        	holder.mPageName.setTextColor(mContext.getResources().getColor(R.color.thumbnail_name_color));
        }
        
        holder.mThumbNail.setImageMatrix(mMatrix);
        holder.mThumbNail.setScaleType(ScaleType.MATRIX);
        
        if (obj != null) {
        	
	        switch(obj.mType) {
	        case DefaultSettings.ms_Json_BookPageType_cover:
	        	if (!TextUtils.isEmpty(obj.mThumb)) {
	        		Bitmap thumbnail = safegenerateForeBitmap(obj.mThumb);
	        		if (thumbnail != null) {
	        			holder.mThumbNail.setImageBitmap(thumbnail);
	        		}
	        	}
	        	if (obj.mIndex == 1) {
	        		 holder.mPageName.setText(R.string.cover);
	        	} else {
	        		holder.mPageName.setText(R.string.cover_two);
	        	}
	        	break;
	        case DefaultSettings.ms_Json_BookPageType_backover:
	        	if (!TextUtils.isEmpty(obj.mThumb)) {
	        		Bitmap thumbnail = safegenerateForeBitmap(obj.mThumb);
	        		if (thumbnail != null) {
	        			holder.mThumbNail.setImageBitmap(thumbnail);
	        		}
	        	}
	        	holder.mPageName.setText(R.string.backcover);
	        	break;
	        case DefaultSettings.ms_Json_BookPageType_page:
	        	
	        	if (!TextUtils.isEmpty(obj.mThumb)) {
	        		Bitmap thumbnail = safegenerateForeBitmap(obj.mThumb);
	        		if (thumbnail != null) {
	        			holder.mThumbNail.setImageBitmap(thumbnail);
	        		}
	        	}
	        	holder.mPageName.setText(String.valueOf(obj.mPageNumber));
	        	break;
            case DefaultSettings.ms_Json_BookPageType_supplement:
                if (!TextUtils.isEmpty(obj.mThumb)) {
                    Bitmap thumbnail = safegenerateForeBitmap(obj.mThumb);
                    if (thumbnail != null) {
                        holder.mThumbNail.setImageBitmap(thumbnail);
                    }
                }
                holder.mPageName.setText(R.string.supplement);
                break;
	        case DefaultSettings.ms_Json_BookPageType_bookquiz:
	        	
	        	 holder.mThumbNail.setScaleType(ScaleType.FIT_XY);
	        	if (!TextUtils.isEmpty(obj.mThumb)) {
	        		Bitmap thumbnail = safegenerateForeBitmap(obj.mThumb);
	        		if (thumbnail != null) {
	        			holder.mThumbNail.setImageBitmap(thumbnail);
	        		}
	        	} else {
	        		holder.mThumbNail.setImageResource(R.drawable.quiz_thumb);
	        	}
	        	holder.mPageName.setText(R.string.quiz);
	        	break;
	        default:
	        	holder.mPageName.setText("");
	        	break;
	        
	        }


        }
        
		return convertView;
	}
	
    private class ViewHolder {
        ThumbnailView   mThumbNail;
        //ImageView   mNormalMask;        
        TextView mPageName;
    }
    
	private Bitmap safegenerateForeBitmap(String simgpath) {
		Bitmap bmp = null;
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inPreferredConfig = Bitmap.Config.RGB_565;
		options.inJustDecodeBounds = false; 
		bmp = ImageUtils.safedecodebitmapfile(simgpath, options);
		if (bmp != null) {
			//MyLog.d("KOK getnewforebitmap " + simgpath + " w = " +bmp.getWidth() + " h = " +bmp.getHeight());
		}
		return bmp;
	}
	
	public void setSelection(int selection) {
		mSelectedIndex = selection;
		notifyDataSetChanged();
	}
	
	public int getSelection() {
		return mSelectedIndex;
	}
	

}
